* Prepare dep vars + firm level patenting; subset rel. firms
* -----------------------------------------------------------
*load only relevant variables
use BvD year industry k${depvar}_${ttt} k${depvar}_${ttt}0 kNOT_${depvar}_${ttt} /// 
kNOT_${depvar}_${ttt}0 spill${depvar}${ttt}_1995_a spillN${depvar}${ttt}_1995_a ///
spill${depvar}${ttt}_1995_a0 spillN${depvar}${ttt}_1995_a0 lse_id ${depvar}_${ttt} ///
country_shr_1995 missing_weights_1995 maxweight_1995 missing_spill_weights_1995 using ${final_dir}/regression_dataset${weight_window}${tf}.dta, clear

* Subset to baseline regression firmsand clone7rename
mmerge BvD using ${final_dir}/bvd_list_regfirms_auto95.dta, unmatched(none) 
keep if year >= 1995 & year <= 2011
local yy=1995
clonevar spilloversown = spill${depvar}${ttt}_`yy'_a
clonevar spilloversother = spillN${depvar}${ttt}_`yy'_a
clonevar spilloversownzero = spill${depvar}${ttt}_`yy'_a0
clonevar spilloversotherzero = spillN${depvar}${ttt}_`yy'_a0
clonevar stockown = k${depvar}_${ttt} 
clonevar stockownzero = k${depvar}_${ttt}0  
clonevar stockother = kNOT_${depvar}_${ttt} 
clonevar stockotherzero = kNOT_${depvar}_${ttt}0
bys lse_id : egen _total_${depvar}_${ttt}_`yy' = sum(${depvar}_${ttt}) if year>=`yy'+2 & year <= 2009+2
bys lse_id : egen total_${depvar}_${ttt}_`yy' = max(_total_${depvar}_${ttt}_`yy')
sort lse_id year
tab year, gen(YY_)
egen yearctry = group(year country_shr_1995) if year <= 2009
egen yearindustry = group(year industry) if year <= 2009

keep BvD year missing_weights_1995 missing_spill_weights_1995 maxweight_1995 total_${depvar}_${ttt}_1995 lse_id yearctry yearindustry ${depvar}_${ttt} spilloversown spilloversother spilloversownzero spilloversotherzero stockown stockownzero stockotherzero
save ${monte_data_dir}/bvd_year_firmdata_country_`rname'.dta, replace 
keep BvD 
duplicates drop
tempfile bvd_list
save `bvd_list', replace


* Store firm weights:	
* -------------------------

use ${dataset_dir}/weights/bvdid_pat_weights_EPtr_from1970_1995_orbis2017_tfacit1.dta, clear
mmerge BvD using `bvd_list', unmatched(none)
drop _m
reshape long share2_all_1995_, i(BvDIDnumber) j(country) string
gsort +BvD -share2_all_1995_
gen dom = 0
*mark highest weight as homecountry's weigth
by BvD: replace dom = 1 if _n == 1
gsort BvDIDnumber country
by BvDIDnumber: gen country_id = _n 
drop country
save ${monte_data_dir}/bvd_country_firmweights_country_`rname'.dta, replace

* Prepare dataset of macroeconomic variables for reshuffling:
* -----------------------------------------------------------

* LSW
use ${dataset_dir}/indep_vars/lswages_wide_MANUF.dta, clear
keep year lswMP*
reshape long lswMP_, i(year) j(country) string
save ${monte_data_dir}/ctry_year_macrodata_`rname'.dta, replace
sleep 2000

* HSW
use ${dataset_dir}/indep_vars/hswages_wide_MANUF.dta, clear 
keep year hswMP*
reshape long hswMP_, i(year) j(country) string
merge 1:1 year country using ${monte_data_dir}/ctry_year_macrodata_`rname'.dta, nogen
sleep 2000
save ${monte_data_dir}/ctry_year_macrodata_`rname'.dta, replace
sleep 2000

* GDPPC
use ${dataset_dir}/indep_vars/gdp_percapita_wide_MANUF.dta, clear 
keep year gdppcMP*
reshape long gdppcMP_, i(year) j(country) string
merge 1:1 year country using ${monte_data_dir}/ctry_year_macrodata_`rname'.dta, nogen
sleep 2000
save ${monte_data_dir}/ctry_year_macrodata_`rname'.dta, replace
sleep 2000

* GDPGAP
use ${dataset_dir}/indep_vars/loggdp_gap_wide_MANUF.dta, clear 
keep year lngdpgap*
reshape long lngdpgap_, i(year) j(country) string
merge 1:1 year country using ${monte_data_dir}/ctry_year_macrodata_`rname'.dta, nogen
sleep 2000
save ${monte_data_dir}/ctry_year_macrodata_`rname'.dta, replace
sleep 2000

* VAEMP
use ${dataset_dir}/indep_vars/vaemployee_wide_MANUF.dta, clear
keep year vaempMP*
reshape long vaempMP_, i(year) j(country) string
merge 1:1 year country using ${monte_data_dir}/ctry_year_macrodata_`rname'.dta, nogen
sleep 2000
save ${monte_data_dir}/ctry_year_macrodata_`rname'.dta, replace
sleep 2000

* store data
keep if year >= 1995 & year <= 2009
sort country year
egen country_id = group(country)
clonevar country_rand_id = country_id
save ${monte_data_dir}/ctry_year_macrodata_`rname'.dta, replace
sleep 2000